package com.fangzhi.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fangzhi.mobel.AdminAccountRoleDeptRelation;
import com.fangzhi.mobel.ProductType;
import com.fangzhi.mobel.enums.GlobalCodeEnum;
import com.fangzhi.mobel.io.SaveProductTypeIo;
import com.fangzhi.mobel.vo.LoginVo;
import com.fangzhi.service.AdminAccountRoleDeptRelationService;
import com.fangzhi.service.ProductTechnologyService;
import com.fangzhi.service.ProductTypeService;
import com.fangzhi.utils.GlobalParam;
import com.fangzhi.utils.ResponseResult;
import org.assertj.core.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/product")
@RestController
public class ProductTypeController {

    @Autowired
    private ProductTypeService productTypeService;
    @Autowired
    private ProductTechnologyService productTechnologyService;
    @Autowired
    private AdminAccountRoleDeptRelationService adminAccountRoleDeptRelationService;

    /**
     * 新增产品或分类
     * @param io
     * @return
     */
    @PostMapping("/saveProductType")
    public ResponseResult saveProductType(@RequestBody SaveProductTypeIo io){
        // 验证账号是否是管理员权限
        LoginVo loginVo = GlobalParam.LOGIN_ACCOUNT_INFO.get();
        List<Integer> list = java.util.Arrays.asList(1, 6);
        AdminAccountRoleDeptRelation one = adminAccountRoleDeptRelationService.getOne(Wrappers.<AdminAccountRoleDeptRelation>lambdaQuery()
                .eq(AdminAccountRoleDeptRelation::getAdminAccounntId, loginVo.getId())
                .in(AdminAccountRoleDeptRelation::getRoleId, list));
        if (null == one)
            return ResponseResult.systemException(GlobalCodeEnum.GL_FAIL_9997);
        if (null != io.getId() && io.getId() > 0)
            return productTypeService.updateProductType(io);
        return productTypeService.saveProductType(io);
    }

    /**
     * 产品树
     * @return
     */
    @GetMapping("/productList")
    public ResponseResult productList(){
        return productTypeService.productList();
    }

    /**
     * 根据id查询所有内容
     * @return
     */
    @GetMapping("/productListById")
    public ResponseResult productListById(@RequestParam(defaultValue = "0") Integer id
                                        , @RequestParam(defaultValue = "") String name
                                        , @RequestParam(defaultValue = "1") Integer pageNo
                                        , @RequestParam(defaultValue = "20") Integer pageSize){
        return productTypeService.productListById(id, name, pageNo, pageSize);
    }

    /**
     * 下线产品分类
     * @param id
     * @return
     */
    @GetMapping("/removeProductType")
    public ResponseResult removeProductType(@RequestParam Integer id){
        // 验证账号是否是管理员权限
        LoginVo loginVo = GlobalParam.LOGIN_ACCOUNT_INFO.get();
        List<Integer> list = java.util.Arrays.asList(1, 6);
        AdminAccountRoleDeptRelation one = adminAccountRoleDeptRelationService.getOne(Wrappers.<AdminAccountRoleDeptRelation>lambdaQuery()
                .eq(AdminAccountRoleDeptRelation::getAdminAccounntId, loginVo.getId()).in(AdminAccountRoleDeptRelation::getRoleId, list));
        if (null == one)
            return ResponseResult.systemException(GlobalCodeEnum.GL_FAIL_9997);
    	return productTypeService.removeProductType(id);
    }

    /**
     * 获取产品基本信息（产品名称，价格）
     * @param id
     * @return
     */
    @GetMapping("/queryProductDetail")
    public ResponseResult queryProductDetail(@RequestParam Integer id){
        return productTypeService.queryProductDetail(id);
    }

    @GetMapping("/technologyList")
    public ResponseResult technologyList(){
        return productTechnologyService.technologyList();
    }
}
